﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class MasterPage_Visitor : System.Web.UI.MasterPage
{
    SqlConnection con;
    //Connect to database   
    void connect()
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["OPOConnectionString"].ToString());
        con.Open();
        //if (con != null)
        //{
        //    Response.Write("Connect Successful.");
        //}

    }
    //Login 
    void login()
    {
        try
        {

            string strSelect = "Select * From Account Where Username = @UserName And Password = @Password And RoleID=@RoleID ";
            SqlCommand cmd = new SqlCommand(strSelect, con);
            cmd.Parameters.AddWithValue("@UserName", txtUsername.Text);
            cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
            cmd.Parameters.AddWithValue("@RoleID", "3");
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable login = new DataTable();
            da.Fill(login);
            if (login.Rows.Count > 0)
            {
                Session["Username"] = txtUsername.Text;
                Session["AccID"] = login.Rows[0][0].ToString();
                Response.Redirect("~/Visitor/index.aspx");
            }
            else
            {
                 Response.Redirect("~/Visitor/LoginFail.aspx");
            }

        }
        catch (Exception e)
        {
            Response.Write(e.Message);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        connect();
        txtPassword.Text = "abc123";
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        login();
    }
    protected void txtUsername_TextChanged(object sender, EventArgs e)
    {
       
    }
    protected void txtPassword_TextChanged(object sender, EventArgs e)
    {
       
    }
}
